<template>
  <div>
    <van-icon :name="value === 1 ? 'good-job' : 'good-job-o' "
              :class="{ like: value === 1 }"
              :loading="loading"
              @click="onLike" />
  </div>
</template>

<script>
import { addLike, deleteLike } from '@/api/article.js'
export default {
  name: 'LikeArticle',
  props: {
    value: {
      // type: Number,
      required: true
    },
    articleId: {
      // type: [Number, String, Object],
      required: true
    }
  },
  data () {
    return {
      loading: false
      // value: false
      // articleId: 1
    }
  },
  methods: {
    async onLike () {
      // console.log(this.value)
      this.loading = true
      try {
        let status = -1
        if (this.value === 1) {
          await deleteLike(this.articleId)
        } else {
          await addLike(this.articleId)
          status = 1
        }
        this.$emit('input', status)
        this.$toast.success(status === 1 ? '点赞成功' : '取消点赞')
      } catch (err) {
        this.$toast.fail('操作失败， 请重试！')
      }
      this.loading = false
    }
  }
}
</script>

<style lang="less" scoped>
/deep/ .van-icon {
  color: #e21918;
}
</style>
